﻿using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;
using Libraries.Common;
using Libraries.Entities;

namespace Libraries.Repositories
{
    public class DepartmentRepository
    {
        private static Department ParseToEntityDepartment(IDataRecord dataReader)
        {
            if (dataReader != null)
            {
                return new Department
                {
                    DepartmentId = (int)dataReader["DepartmentId"],
                    DepartmentName = (string)dataReader["DepartmentName"],
                    ParentDepartmentId = (int)dataReader["ParentDepartmentId"]
                };
            }
            return null;
        }

        public Collection<Department> ListAllDepartment(int locationId = 0)
        {
            var parameters = new Collection<SqlParameter>
                                 {
                                     new SqlParameter("@LocationId", locationId)
                                 };
            Collection<Department> list = null;
            if (!string.IsNullOrEmpty(ProcedureConfiguration.PagingAccountCourse))
            {
                using (var result = RepositorySql.SqlHelper.ExecuteReader(ProcedureConfiguration.Departments, parameters, ConnectionConfiguration.ConnectionString))
                {
                    if (result != null)
                    {
                        list = new Collection<Department>();

                        while (result.Read())
                        {
                            list.Add(ParseToEntityDepartment(result));
                        }
                    }
                }
            }
            return list;
        }
		public Collection<Department> ListAllDepartmentPaging(int pageSize, int currentPage, int locationId)
        {
            Collection<Department> list = null;
            var parameters = new Collection<SqlParameter>
                                 {
                                     new SqlParameter("@PageSize",pageSize),
                                     new SqlParameter("@CurrentPage",currentPage),
                                     new SqlParameter("@LocationId",locationId)
                                 };
            if (!string.IsNullOrEmpty(ProcedureConfiguration.ListAllDepartmentPaging))
            {
                using (var result = RepositorySql.SqlHelper.ExecuteReader(ProcedureConfiguration.ListAllDepartmentPaging, parameters,ConnectionConfiguration.ConnectionString))
                {
                    if (result != null)
                    {
                        list = new Collection<Department>();
                        while (result.Read())
                        {
                            list.Add(new Department
                                         {
                                             DepartmentId = result["DepartmentId"].ToInt32OrDefault(),
                                             DepartmentName = result["DepartmentName"].ToStringOrDefault(),
                                             DepartmentFullName = result["DepartmentFullName"].ToStringOrDefault(),
                                             ParentDepartmentId = result["ParentDepartmentId"].ToInt32OrDefault(),
                                             Descriptions = result["Descriptions"].ToStringOrDefault(),
                                             CreateBy = result["CreatedBy"].ToInt32OrDefault(),
                                             CreateDate = result["CreateDate"].ToDateTimeOrDefault(),
                                             ModifiedBy = result["ModifiedBy"].ToInt32OrDefault(),
                                             ModifiedDate = result["ModifiedDate"].ToDateTimeOrDefault(),
                                             TotalRow = result["TotalRow"].ToInt32OrDefault()
                                         });
                        }
                    }
                }
            }
            return list;
        }
    }
}